/**
 * DataManager.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.data;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

/**
 * The Class DataManager.
 * 
 * @author Gerson Groth
 */
public class DataManager {

	/** The logger. */
	private Logger logger = Logger.getLogger(DataManager.class);

	/** The instance. */
	private static DataManager INSTANCE = new DataManager();

	/**
	 * Gets the instance.
	 * 
	 * @return the instance
	 */
	public static DataManager getINSTANCE() {
		return INSTANCE;
	}

	/**
	 * Save data.
	 */
	public void saveData() {
		try {
			// use buffering
			OutputStream file = new FileOutputStream(DataConstants.DATA_PATH);
			OutputStream buffer = new BufferedOutputStream(file);
			ObjectOutput output = new ObjectOutputStream(buffer);
			try {
				output.writeObject(DataFile.getInstance());
				Data data = Data.getInstance();
				data.setBrowser(DataFile.getInstance().getBrowser());
				if (!"".equals(DataFile.getInstance().getPassword())
						&& !"".equals(DataFile.getInstance().getUsername())) {
					data.setPassword(DataManager.decryptPass(DataFile.getInstance()
							.getPassword()));
					data.setUserName(DataFile.getInstance().getUsername());
					data.setLogin(true);
				} else {
					data.setLogin(false);
					data.setPassword("");
					data.setUserName("");
				}
			} finally {
				output.close();
			}
		} catch (IOException ex) {
			logger.error("Cannot perform output.", ex);
		}
	}

	/**
	 * Load data.
	 * 
	 * @return the data file
	 */
	public DataFile loadData() {
		try {
			// use buffering
			InputStream file = new FileInputStream(DataConstants.DATA_PATH);
			InputStream buffer = new BufferedInputStream(file);
			ObjectInput input = new ObjectInputStream(buffer);
			try {
				// deserialize the List
				DataFile.getInstance().setInstance(input.readObject());
				Data data = Data.getInstance();
				data.setBrowser(DataFile.getInstance().getBrowser());
				
				if (!"".equals(DataFile.getInstance().getPassword())
						&& !"".equals(DataFile.getInstance().getUsername())) {

					data.setPassword(DataManager.decryptPass(DataFile.getInstance()
							.getPassword()));
					data.setUserName(DataFile.getInstance().getUsername());
					data.setLogin(true);
				} else
					data.setLogin(false);
				
				// decryptPass(Data.getInstance().getPassword());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				input.close();
			}
		} catch (FileNotFoundException e) {
			logger.warn("Data file not found: " + e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return DataFile.getInstance();
	}

	/**
	 * Crypt the password
	 * 
	 * @param pass
	 *            the pass
	 * @return the string
	 */
	public static String cryptPass(String pass) {
		byte[] encodedBytes = Base64.encodeBase64(pass.getBytes());

		return new String(encodedBytes);

	}

	/**
	 * Decrypt the password.
	 * 
	 * @param encodedBytes
	 *            the encoded bytes
	 * @return the string
	 */
	public static String decryptPass(String encodedBytes) {
		byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
		return new String(decodedBytes);
	}
}
